// pages/optional/my/index.js
const {
  schoolReportList
} = require('../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    pageNo: 1,
    pageSize: 20,
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()
    let that = this;
    getApp().watch(that.callBack)
  },
  callBack: function (val) {
    console.log(val)
    this.setData({
      loading: val
    })
  },
  goto(e) {
    const {value}=e.currentTarget.dataset
    const {
      firstSelect,
      province,
      provinceId,
      secondSelect,
      selectedIds,
      selectedList,
      type,
      year,id
    } = value
    if (type == 'custom') {
      const subjects=(firstSelect?firstSelect+',':'')+secondSelect
      const params = {
        firstSelect,
        secondSelect,
        provinceId,
        province,
        year,
        subjects,
        planMatchId:id
      }
      wx.navigateTo({
        url: '/pages/optional/analyse/index?params=' + encodeURIComponent(JSON.stringify(params)),
      })
    } else {
      var selectedIntIds=selectedIds.split(',').map((item)=>{
        return parseInt(item)
      })
      const params = {
        selectedList,
        mode: type,
        selectedIds:selectedIntIds,
        subjects: secondSelect,
        year,
        provinceId,
        province,
        planMatchId:id
      }
      wx.navigateTo({
        url: '/pages/optional/report/index?params=' + encodeURIComponent(JSON.stringify(params)),
      })
    }

  },
  async getList() {
    const {
      pageNo,
      pageSize
    } = this.data
    const params = {
      pageNo,
      pageSize
    }
    const {
      result
    } = await schoolReportList(params)
    if (result.records.length == 0) {
      return
    }
    result.records.map((item) => {
      item.secondSelectChar = item.secondSelect.replaceAll(',', '+')
    })
    this.setData({
      ['list[' + (pageNo - 1) + ']']: result.records,
      pageNo: pageNo + 1,
    })
  },

  onReachBottom: function () {
    const {
      list,
      pageSize
    } = this.data
    const count = list.length - 1
    if (list[count].length < pageSize) {
      return
    }
    this.getList()
  },
})